//
//  AfterTeaVc.m
//  Shengzhuli
//
//  Created by FXGuo on 2018/3/19.
//  Copyright © 2018年 kamangkeji. All rights reserved.
//

#import "AfterTeaVc.h"
#import "AcNewFlowViewCell.h"
#import "CustomerCell.h"
#import "ActivityDetailModel.h"
#import "PersonInfo.h"
#import "TeaImgCell.h"
#import "TeaTitleCell.h"
#import "SZLAccountHelper.h"
#import "PersonNetWork.h"
#import "ClubNetwork.h"
#import "MBProgressHUD+MJ.h"
#import "SZLTabBarViewController.h"
#import "TeaTitleCell.h"
#import "TeaCardCell.h"
#import "TeaDetailCell.h"
#import "TeaFlowCell.h"
#import <Masonry.h>
#import "ShareContentModel.h"
#import "ShareTool.h"
#import "NSDate+Extension.h"

@interface AfterTeaVc ()<UITableViewDataSource,UITableViewDelegate>


@property (nonatomic,strong) UIButton *shareBtn;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,weak) UIButton *bottomBtn;
@property (nonatomic,strong) ActivityDetailModel *detailModel;
@property (nonatomic,strong) PersonInfo *info;
@property (nonatomic,strong) NSString *cardStr;

@end

@implementation AfterTeaVc

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    if (self.type == 1) {
        self.title = @"下午茶";
    }else if (self.type == 1) {
        self.title = @"结缘晚宴";
    }
    self.cardStr = @"尊敬的会员：\n     您好！\n     玩转地球结缘晚宴即将开始。\n     如今是一个资源整合的时代，是一个优势互补更是抱团取暖的时代。我们深知宇宙生命的法则就是当你成为爱，一切资源都将涌向你！\n     玩转地球结缘晚宴的宗旨就是为了各界人士能够在吃喝中结交朋友，在玩中实现跨界的合作。玩转地球就是人脉的加工厂，是快乐的制造商，您存给我100个陌生人，我还给您1000个永远的朋友。\n     来吧，加入我们，让我们在这美食盛宴中，结缘更多志同道合的朋友跟合作伙伴。";
    if ([SZLAccountHelper isAlreadyLogin]) {
        [self getPersonInfo];
    }
    [self setupView];
    [self getNearAcNewDetail];
}

- (void)setupView
{
    UIButton *shareBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 25,25)];
    shareBtn.titleLabel.font = Font(15);
    [shareBtn setTitleColor:RGBColor(255, 121, 201) forState:UIControlStateNormal];
    [shareBtn addTarget:self action:@selector(shareClick:) forControlEvents:UIControlEventTouchUpInside];
    self.shareBtn = shareBtn;
    
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSpacer.width = -10;
    self.navigationItem.rightBarButtonItems = @[negativeSpacer,[[UIBarButtonItem alloc]initWithCustomView:shareBtn]];
    
    UIButton *bottomBtn = [[UIButton alloc]init];
    [bottomBtn setTitle:@"立 即 报 名" forState:UIControlStateNormal];
    bottomBtn.titleLabel.font = Font(19);
    bottomBtn.backgroundColor = RGBA(0, 0, 0, 0.8);
    bottomBtn.tag = 1;
    [bottomBtn addTarget:self action:@selector(shareClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:bottomBtn];
    self.bottomBtn = bottomBtn;
    [bottomBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self.view);
        make.height.mas_equalTo(50);
    }];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.view).offset(-50);
    }];
}
- (void)shareClick:(UIButton *)btn
{
    if ([SZLAccountHelper shouldLogin]) return;
    if (btn.tag == 1) {
        [self reportClick];
        return;
    }
    ShareContentModel *model = [[ShareContentModel alloc]init];
    model.title = self.detailModel.title;
    model.pageUrl = self.detailModel.webActivityUrl;
    model.content = self.detailModel.flow;
    model.sharePicUrl = [self.detailModel.activityPictureUrl componentsSeparatedByString:@"#"].firstObject;
    model.idd = [NSString stringWithFormat:@"%ld",(long)self.ID];
    [ShareTool share:model];
}
#pragma mark - Table view data delegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0.01;
    }
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else if (section == 1) {
        return 3;
    }else if (section == 2) {
        return 2;
    }
    return 1+self.detailModel.guestList.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 300*ScreenW/750.0;
    }else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            return 44;
        }else if (indexPath.row == 1) {
            return [TeaCardCell calculateStrHeight:self.cardStr];
        }else if (indexPath.row == 2) {
            return [TeaDetailCell calculateDetailHeight:self.detailModel];
        }
    }else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            return 44;
        }
        TeaFlowCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([TeaFlowCell class])];
        return [cell calculateCellH:self.detailModel];
    }
    if (indexPath.row == 0) {
        return 44;
    }
    return 90;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        TeaImgCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([TeaImgCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (self.type == 1) {
            cell.imgStr = @"banner下午茶";
        }else {
            cell.imgStr = @"banner晚宴";
        }
        return cell;
    }else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            TeaTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([TeaTitleCell class])];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.title = self.detailModel.title;
            return cell;
        }else if (indexPath.row == 1) {
            TeaCardCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([TeaCardCell class])];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.str = self.cardStr;
            return cell;
        }else if (indexPath.row == 2) {
            TeaDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([TeaDetailCell class])];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.model = self.detailModel;
            return cell;
        }
    }else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            TeaTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([TeaTitleCell class])];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.title = @"具体流程";
            return cell;
        }
        TeaFlowCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([TeaFlowCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = self.detailModel;
        return cell;
    }
    if (indexPath.row == 0) {
        TeaTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([TeaTitleCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.title = @"受邀嘉宾";
        return cell;
    }
    CustomerCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CustomerCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.detailModel.guestList[indexPath.row-1];
    return cell;
}

//活动详情
- (void)getNearAcNewDetail
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    if ([SZLAccountHelper isAlreadyLogin]) {
        parame[@"token"] = [SZLAccountHelper shareInstance].token;
    }
    parame[@"id"] = self.ID;
    [self showClearBgWaiter];
    [ClubNetwork getNearAcNewDetailWithParame:parame response:^(BOOL success, NSString *msg,ActivityDetailModel *model) {
        [self hideWaiter];
        if (success) {
            self.detailModel = model;
            [self.shareBtn setImage:GetImage(@"下午茶分享") forState:UIControlStateNormal];
            [self.tableView reloadData];
        }else {
            [MBProgressHUD showError:msg];
        }
    }];
}
//活动报名
- (void)reportClick
{
    if ([SZLAccountHelper shouldLogin]) return;
    NSDate *holdDate = [NSDate stringToDate:[NSString stringWithFormat:@"%lld",[self.detailModel.holdDate longLongValue]/1000]];
    BOOL isPast = [holdDate isInPast];
    if (isPast) {
        [MBProgressHUD showError:@"活动已举办完成" toView:self.view];
        return;
    }
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"activityId"] = @(self.detailModel.ID);
    parame[@"registrationName"] = [SZLAccountHelper shareInstance].name;
    parame[@"registrationPhone"] = [SZLAccountHelper shareInstance].mobilePhone;
    [self showClearBgWaiter];
    [ClubNetwork nearActivityReportWithParame:parame response:^(BOOL success, NSString *msg) {
        [self hideWaiter];
        if (success) {
            [MBProgressHUD showSuccess:@"报名成功"];
            [self.tableView reloadData];
        }else {
            [MBProgressHUD showError:msg];
        }
    }];
}

//获取一下个人信息
- (void)getPersonInfo
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    [self showClearBgWaiter];
    [PersonNetWork getPersonInfoWithParame:parame response:^(BOOL success, NSString *msg,NSString *errorCode,PersonInfo *info) {
        [self hideWaiter];
        [self hideReloadView];
        if (success) {
            self.info = info;
        }else{
            [MBProgressHUD showError:msg toView:self.view];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                if ([errorCode isEqualToString:NoLoginStatus]) {
                    [self.navigationController popToRootViewControllerAnimated:YES];
                    SZLTabBarViewController *barVc = (SZLTabBarViewController*)[UIApplication sharedApplication].keyWindow.rootViewController;
                    barVc.selectedIndex = 0;
                    [SZLAccountHelper clearAlreadyInfo];
                    [SZLAccountHelper shouldLogin];
                }
            });
        }
    }];
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenW,ScreenH-SafeAreaTopHeight-50) style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.sectionFooterHeight = 0.01;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.backgroundColor = RGBColor(239, 239, 244);
//        _tableView.contentInset = UIEdgeInsetsMake(0, 0, 49, 0);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableFooterView = [[UIView alloc]init];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([TeaImgCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([TeaImgCell class])];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([TeaCardCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([TeaCardCell class])];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([TeaDetailCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([TeaDetailCell class])];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([TeaTitleCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([TeaTitleCell class])];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([TeaFlowCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([TeaFlowCell class])];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([CustomerCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([CustomerCell class])];
    }
    return _tableView;
}

@end
